﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmTimKiemMuonTra : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTimKiemMuonTra()
        {
            InitializeComponent();
        }
        Utilities uti = new Utilities();
        ConnectData conn = new ConnectData();
        ExportToExcel exp = new ExportToExcel();
        private DataTable dtb=null;
        private void FrmTimKiemMuonTra_Load(object sender, EventArgs e)
        {
            //Load combobox nhom ban doc
            DataTable tbNhom = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_MaNhom.DataSource = tbNhom;
            DataRow drLoaiSach = tbNhom.NewRow();
            drLoaiSach["MaNhom"] = "all";
            drLoaiSach["TenNhom"] = "Tất cả";
            tbNhom.Rows.InsertAt(drLoaiSach, 0);
            comb_MaNhom.DisplayMember = "TenNhom";
            comb_MaNhom.ValueMember = "MaNhom";
            comb_MaNhom.SelectedIndex = 0;
            //
            check_ChuaTra.Checked = false;
            check_DaTra.Checked = false;
            check_GiaHan.Checked = false;
            check_QuaHan.Checked = false;
            date_MuonDen.Value = System.DateTime.Now;
            date_MuonTu.Value = System.DateTime.Now;
            date_TraDen.Value = System.DateTime.Now;
            date_TraTu.Value = System.DateTime.Now;
        }
        string getSql()
        {
            string sql = "select distinct a.MaMuonTra,a.MaBanDoc,TenBanDoc, c.MaNhom, TenNhom,count(MaCTMuonTra) as 'SoLuong', NgayMuon, NgayPhaiTra " +
                         "from PhieuMuonTra a, CTMuonTra b,BanDoc c, NhomBanDoc d "+
                         "where a.MaMuonTra=b.MaMuonTra and a.MaBanDoc=c.MaBanDoc and c.MaNhom=d.MaNhom ";
            if (txt_MaMuonTra.Text != "")
                sql += "and a.MaMuonTra like N'%" + txt_MaMuonTra.Text + "%' ";
            if (txt_MaBanDoc.Text != "")
                sql += "and a.MaBanDoc like N'%" + txt_MaBanDoc.Text + "%' ";
            if (comb_MaNhom.SelectedIndex != 0)
                sql += "and c.MaNhom=N'" + comb_MaNhom.SelectedValue.ToString() + "' ";
            if (check_ChuaTra.Checked == true)
                sql += "and NgayTra is null ";
           if (check_DaTra.Checked == true)
                sql += "and NgayTra is not null ";
            if (check_GiaHan.Checked == true)
                sql += "and NgayGiaHan is not null ";
            if (check_QuaHan.Checked == true)
                sql += "and ((NgayTra is not null and NgayTra>NgayPhaiTra)or (NgayTra is null and NgayPhaiTra<GETDATE()))";
            //
            //Ngày tháng mượn
            if (check_MuonTu.Checked == true && check_MuonDen.Checked == false)
                sql += "and NgayMuon>='" + date_MuonTu.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_MuonTu.Checked == false && check_MuonDen.Checked == true)
                sql += "and NgayMuon <='" + date_MuonDen.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_MuonTu.Checked == true && check_MuonDen.Checked == true)
                sql += "and NgayMuon BETWEEN '" + date_MuonTu.Value.ToString("yyyy-MM-dd") + "' and '" + date_MuonDen.Value.ToString("yyyy-MM-dd") + "'";
            //Ngáy tháng trả
            if (check_TraTu.Checked == true && check_TraDen.Checked == false)
                sql += "and NgayTra>='" + date_TraTu.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_TraTu.Checked == false && check_TraDen.Checked == true)
                sql += "and NgayTra <='" + date_TraDen.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_TraTu.Checked == true && check_TraDen.Checked == true)
                sql += "and NgayTra BETWEEN '" + date_TraTu.Value.ToString("yyyy-MM-dd") + "' and '" + date_TraDen.Value.ToString("yyyy-MM-dd") + "'";

            sql += "group by a.MaMuonTra,a.MaBanDoc,TenBanDoc, c.MaNhom, TenNhom,NgayMuon, NgayPhaiTra ";
            return sql;
        }

        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            dgv_HienThi.DataSource = conn.GetDataTable(getSql());
        }

        private void btn_XemChiTiet_Click(object sender, EventArgs e)
        {
            int dong = dgv_HienThi.CurrentCell.RowIndex;
            FrmXemCT_MT frm = new FrmXemCT_MT();
            frm.dk = 1;
            frm.maMT=dgv_HienThi.Rows[dong].Cells["MaMuonTra"].Value.ToString();
            frm.ShowDialog();
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void check_ChuaTra_CheckedChanged(object sender, EventArgs e)
        {
            if (check_ChuaTra.Checked == true)
                check_DaTra.Checked = false;
        }

        private void check_DaTra_CheckedChanged(object sender, EventArgs e)
        {
            if (check_DaTra.Checked == true)
                check_ChuaTra.Checked = false;
        }

        private void dgv_HienThi_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_HienThi);
        }

        private void btn_ExportExcel_Click(object sender, EventArgs e)
        {
            exp.exportExcel_2(dgv_HienThi,dtb,1);
        }

        private void dgv_HienThi_DoubleClick(object sender, EventArgs e)
        {
            btn_XemChiTiet_Click(sender, e);
        }

        private void check_MuonTu_CheckedChanged(object sender, EventArgs e)
        {
            if (check_MuonTu.Checked == true)
                date_MuonTu.Enabled = true;
            else
                date_MuonTu.Enabled = false;
        }

        private void check_MuonDen_CheckedChanged(object sender, EventArgs e)
        {
            if (check_MuonDen.Checked == true)
                date_MuonDen.Enabled = true;
            else
                date_MuonDen.Enabled = false;
        }

        private void check_TraTu_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TraTu.Checked == true)
                date_TraTu.Enabled = true;
            else
                date_TraTu.Enabled = false;
        }

        private void check_TraDen_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TraDen.Checked == true)
                date_TraDen.Enabled = true;
            else
                date_TraDen.Enabled = false;
        }

    }
}